

#include "Line.h"


Line ::~Line(){

//show.closeDisplay();

}


Line :: Line (const MyDisplay &another){


    show = new MyDisplay(another);
}


Line :: Line(const char *fileName){


      ifstream in(fileName);

      	int tempx1,tempy1;
	if(!in)
	{
	    cout <<"\n unable to open file : " <<fileName;
	     return;	
	}
	
	//out <<numRows() <<"   "  <<numCols() <<"\n\n";
	
	    in >>tempx1>>tempy1;
	    
	    x1 = abs(tempx1);
	    y1 = abs(tempy1);
	    
	    in >>tempx1>>tempy1;
	    
	    x2 = abs(tempx1);
	    y2 = abs(tempy1);
	    
	 //   Line();
	    in.close();
	
}



Line :: Line(){

   x1=15;
   y1=15;
   
   x2=125;
   y2=125;
   
   
   show = new MyDisplay();
   // create windows while object is constructed
   show->createWindow( show->getWidth(), show->getHeight(), 0, 0);
   show->createGC(0);

}


Line::Line(int x,int y){

  this->x1=x;
  this->y1=x;
  
  this->x2=y;
  this->y2=y;
  
  show = new MyDisplay();
  // create windows while object is constructed
   show->createWindow(show->getWidth(), show->getHeight(), 0, 0);
   show->createGC(0);
  
  }


Line::Line(int x1,int y1,int x2,int y2){

   this->x1=x1;
   this->y1=y1;
   
   this->x2=x2;
   this->y2=y2;
   
   show = new MyDisplay();
// create windows while object is constructed
   show->createWindow(show->getWidth(), show->getHeight(), 0, 0);
   show->createGC(0);
}


Line::Line(int x){
 
 x1 = y1 = x2 = y2 = x;
 
 show = new MyDisplay();
 // create windows while object is constructed
   show->createWindow(show->getWidth(), show->getHeight(), 0, 0);
  
   show->createGC(0);
   
}



int Line::getdx(){

  return (x2-x1); 
}

int Line::getdy(){

 return (y2-y1);
}


double Line::getSlope(const int &x1,const int &y1, const int &x2, const int &y2){

   return ((double)y2-y1)/(x2-x1);
   
}


double Line::getSlope(){

   return ((double)this->y2- this->y1)/(this->x2 - this->x1);

}



void Line::plot(const double &x,const double &y ){
 
//xlib code goes here including color setting
     show->drawPoint((int)x,(int)y);
     
     
  
}



void Line::drawLine(){

  //double compute;
  int dx,dy;
  int xStart,xEnd,yStart,yEnd;
  
  //double error=0;
  //double dError;
  double slope,deciVar,incE,incNE; 
  int step,temp,temp1;
  bool flag ;

        if(x1 > x2)
	{
		xStart = x2;
		yStart = y2;
		xEnd = x1;
		yEnd = y1;
		
		//swap(x1,y1);
		//swap(x0,y0);
		
	}
	else
	{
		xStart = x1;
		yStart = y1;
		xEnd = x2;
		yEnd = y2;
		
		//swap()
	}
	
	//cout << xStart << " " << xEnd << " " << yStart << " " << yEnd << endl;
	dx = xEnd - xStart;
	dy = yEnd - yStart;
	//cout << dx << " " << dy << endl;
	if(dx < ((-1) * dy))
		temp1 = (-1) * dy;
	else
		temp1 = dy;
	if(dx < temp1)
	{
		
		temp = xStart;
		xStart = yStart;
		yStart = temp;
		temp = xEnd;
		xEnd = yEnd;
		yEnd = temp;
		flag = 0;

	}
	else
		flag = 1;
	
	dx = xEnd - xStart;
	dy = yEnd - yStart;	
	if((flag == 0) && (dx < 0))
	{
		temp = xStart;
		xStart = xEnd;
		xEnd = temp;
		temp = yStart;
		yStart = yEnd;
		yEnd = temp;
	}	
	dx = xEnd - xStart;
	dy = yEnd - yStart;	
	//cout << dx << " " << dy << endl;
	if(dy < 0)
	{
		slope = -1;
		dy = (-1) * dy;
	}                                                                                                                                                                                                                                           
	else
	if(dy == 0)
		slope = 0;
	else
		slope = 1;
	incE = 2 * dy;
	incNE = 2 * (dy - dx);
	deciVar = 2 * dy - dx;
	y = yStart;
	//cout << "deciVar = " << deciVar << " incE = " << incE << " incNE = " << incNE << endl;
	for(x = xStart; x <= xEnd; x++)
	{
		if(flag != 0)
		{
		//	cout << x << " " << y << endl;
			//if((x != 0) && (y !=0))
				show->drawPoint((int)x,(int)y);
			if(deciVar <= 0)
				deciVar += incE;
			else
			{
				deciVar += incNE;
				y +=  slope;
			} 
		}
		else
		{
		//	cout << y << " " << x << endl;
			//if((x != 0) && y!=0)
				show->drawPoint((int)y,(int)x);
			if(deciVar <= 0)
				deciVar += incE;
			else
			{
				deciVar += incNE;
				y += slope;	
			}
		}
	}
  
}

                                       
int Line::swap(int &x,int &y){
 
   int temp;
 
   temp=x;
   x=y;
   y=temp;
 
}


void Line::setX1Y1(int x1,int y1){

   this->x1=x1;
   this->y1=y1;
}

void Line::setX2Y2(int x2,int y2){

   this->x2=x2;
   this->y2=y2;
}


void Line::drawLine(double x1,double y1,double x2,double y2){

    setX1Y1((int)x1,(int)y1);
    setX2Y2((int)x2,(int)y2);
    
    drawLine();

}


void Line :: setCoordinates(const char *fileName){

   
      ifstream in(fileName);

      	
	if(!in)
	{
	    cout <<"\n unable to open file : " <<fileName;
	     return;	
	}
	
	//out <<numRows() <<"   "  <<numCols() <<"\n\n";
	
	    in >>x1>>y1>>x2>>y2;
	    
	    in.close();


}

